<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\SMSConfirmationCode;
use Bright\Aliyun\Sms\Sms;

/**
 * 用户验证控制器
 * Class AuthController
 * @package App\Http\Controllers\Auth
 */
class AuthController extends Controller
{

    /**
     * @var
     */
    public $sms;


    public function __construct(Sms $sms)
    {
        $this->middleware('csrf');
        $this->sms = $sms;
    }

    /**
     * 发送短信验证码
     *
     * @param Request $request
     * @return number[]
     */
    public function postSendSmsCodeByAliyun(Request $request)
    {
        // if (Session::get('captcha') == $request->get('captcha') && $request->get('captcha')) {
        $this->validate($request, [
            'mobile' => 'required|regex:/^(([0-9]{5,25}))$/|unique:users'

        ]);
        $mobile = $request->get('mobile');
        $model = new SMSConfirmationCode();
        $model->mobile = $mobile;
        $model->code = $model->generateSMSConfirmationCode();
        $model->expired = date('Y-m-d H:i:s', time() + 1800);
        $model->save();

        $ip = $request->getClientIp();
        \Log::info($ip . '请求发送注册短信to' . $mobile);
        $params = [
            'name' => '培优策',
            'school' => $model->code . '',
        ];
        $response =  $this->sms->send($mobile,'2018静海随机派位系统','SMS_132396127', $params);
        if ($response->Code == 'OK') {
            return json_encode('success');
        } else {
            return json_encode('overtime');
        }
//        } else {
//            Session::flash('warning', '请输入正确的图片验证码！');
//            $result = json_encode('no_captcha');
//            return $result;
//        }
    }

    /**
     * 发送短信验证码
     *
     * @param Request $request
     * @return number[]
     */
    public function postUpdatePasswordSendSmsCodeByAliyun(Request $request)
    {
        $mobile = $request->get('mobile');
        // if (Session::get('captcha') == $request->get('captcha') && $request->get('captcha')) {
        if ($this->isUnique($mobile)){
            return response()->json('该手机号未注册', 461);
        }

        $model = new SMSConfirmationCode();
        $model->mobile = $mobile;
        $model->code = $model->generateSMSConfirmationCode();
        $model->expired = date('Y-m-d H:i:s', time() + 1800);
        $model->save();

        $ip = $request->getClientIp();
        \Log::info($ip . '请求发送注册短信to' . $mobile);
        \Log::info($ip . '请求发送注册短信to' . $mobile);
        $params = [
            'name' => '培优策',
            'school' => $model->code . '',
        ];
        $response =  $this->sms->send($mobile,'2018静海随机派位系统','SMS_132396127', $params);
        if ($response->Code == 'OK') {
            return json_encode('success');
        } else {
            return json_encode('overtime');
        }
    }

    /**
     * check a mobile if is unique in user table
     *
     * @param $mobile
     * @param null $userId
     * @return bool
     */
    public function isUnique($mobile, $userId = null)
    {
        $model = DB::table('users')->where('mobile', $mobile)->when($userId, function ($query) use ($userId){
            return $query->where('id', '!=', $userId);
        })->first();

        if ($model){
            return false;
        }else{
            return true;
        }
    }


}
